<%- include('head') -%>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashBoard">首页</a>
      <a><cite>商品管理</cite></a>
    </span>
    <div>
      <a href="/itemAdd" class="layui-btn" style="margin-top: 20px;">增加商品</a>
    </div>

    <table class="layui-table">
      <thead>
        <tr>
          <th>商品名称</th>
          <th>商品价格</th>
          <th>商品描述</th>
          <th>销量</th>
          <th>封面</th>
          <th>上架时间</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <% for(var i=0;i<items.length;i++){ %>
          <tr>
            <td>
              <%= items[i].name %>
            </td>
            <td>
              ￥ <%= items[i].price %>
            </td>
            <td title="<%= items[i].desc %>">
              <%= items[i].desc? (items[i].desc.length>10? items[i].desc.substr(0,10)+'...' :items[i].desc): '暂无描述' %>
            </td>
            <td>
              <%= items[i].sales %>
            </td>
            <td>
              <img width="100"
                src="<%= items[i].thumb?items[i].thumb:'https://img0.baidu.com/it/u=1834701702,1210097260&fm=253&fmt=auto&app=138&f=GIF?w=370&h=260' %>"
                alt="">

            </td>
            <td>
              <%= items[i].createAt %>
            </td>
            <td>
              <a href="/itemUpdate?id=<%= items[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
              <button data-id="<%= items[i]._id %>" class="layui-btn del layui-btn-danger layui-btn-xs">删除</button>
            </td>
          </tr>
          <%}%>
      </tbody>
    </table>
    <div id="page">
    </div>
  </div>
  <script src="/static/lib/layui/layui.js"></script>
  <script>
    layui.use(['laypage', 'jquery', 'layer'], function () {
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;
      
      // 渲染分页组件
      laypage.render({
        elem: 'page'
        , curr: <%= page %>
        , count: <%= total %> //数据总数，从服务端得到
        , limit: 4
        , jump: function (obj, first) {
          //首次不执行
          if (!first) {
            //do something
            location.href = "/itemLists?page=" + obj.curr + "&pageSize=" + obj.limit;
          }
        }
      });
    // 删除商品
    $('.del').click(function () {
      // 弹出确认框 进行删除
        var _this = this;
        layer.confirm('该操作会彻底删除商品，确定继续吗?',
          {
            icon: 3,
            title: '提示'
          },
          function (index) {
            //do something
            $.ajax({
              url: '/delItem',
              data: {
                id: _this.dataset.id
              },
              success: function(res){
                if(res.code === 0) {
                  layer.msg(res.msg, {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  }, function(){
                    // 删除成功刷新页面
                    history.go(0);
                  });  
                }
              }
            })
            layer.close(index);
          }
        )
      })
    })
  </script>
  </body>

  </html>